package net.ithe.springbootwebquickstart;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ResponseController {
    /**
     * 方式一 基于HttpServletResponse设置响应信息
     * @param response
     */
    @RequestMapping("/response")
    public void response(HttpServletResponse response) {
        //1.设置响应状态码
        response.setStatus(401);
        //2.设置响应头
        response.setHeader("itheima","666");
        //3.设置响应体
        try {
            response.getWriter().println("<h1>hello response</h1>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 方式二 基于ResponseEntity设置响应信息-spring方式
     * @return
     */
    @RequestMapping("/response2")//spring方式设置响应
    public ResponseEntity<String> response2() {
       return ResponseEntity
               .status(401)//响应状态
               .header("javaweb","666")//响应头
               .body("<h1>hello response2</h1>");//响应体
    }
}
